package com.fanbai.score.user.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.fanbai.score.R;
import com.fanbai.score.common.Utils.JsonUtils;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.Utils.log.YD_Log;
import com.fanbai.score.common.activity.WebView_Activity;
import com.fanbai.score.common.base.BaseActivity;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.user.bean.HeadImage_Bean;
import com.fanbai.score.user.fragemnt.Act_Fragment;
import com.fanbai.score.user.fragemnt.Elegant_Fragment;
import com.fanbai.score.user.fragemnt.Home_Fragment;
import com.fanbai.score.user.fragemnt.MyInfo_Fragment;
import com.fanbai.score.user.http.UserRequestNetwork_Utils;

import java.util.List;


public class UserMainActivity extends BaseActivity implements TabLayout.OnTabSelectedListener{

    private SectionsPagerAdapter mSectionsPagerAdapter;

    private ViewPager mViewPager;

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a Home_Fragment (defined as a static inner class below).
            switch (position) {
                case 0:
                    return Home_Fragment.newInstance(position + 1);
                case 1:
                    return Elegant_Fragment.newInstance(position + 1);
                case 2:
                    return Act_Fragment.newInstance(position + 1);
                case 3:
                    return MyInfo_Fragment.newInstance(position + 1);
            }
            return Home_Fragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 4;
        }

    }

    TabLayout tabLayout;

    @Override
    protected int getLayoutID() {
        return R.layout.user_activity_main;
    }

    @Override
    public String getTitleName() {
        return null;
    }

    @Override
    protected void initView() {
        toolbar.setVisibility(View.GONE);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.addTab(tabLayout.newTab().setText("贵人圈").setIcon(R.mipmap.icon_user_home_h));
        tabLayout.addTab(tabLayout.newTab().setText("贵人钱包").setIcon(R.mipmap.icon_user_qianbao_h));
        tabLayout.addTab(tabLayout.newTab().setText("活动").setIcon(R.mipmap.icon_user_huodong_h));
        tabLayout.addTab(tabLayout.newTab().setText("设置").setIcon(R.mipmap.icon_user_geren_h));
//        tabLayout.setupWithViewPager(mViewPager);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(this);

    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initListener() {

    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK){
            Bundle bundle = data.getExtras();
            if (bundle != null) {
                String result=bundle.getString("result");
                YD_Log.e("扫码结果："+result);
                if(result.startsWith("http")){
                    bundle = new Bundle();
                    bundle.putString("url", result);
                    ctx.jumpToActivity(WebView_Activity.class, bundle);
                }else{
                    String oid = JsonUtils.getString(result, "oid");
                    bundle = new Bundle();
                    bundle.putString("oid", oid);
                    jumpToActivity(ShoppingDetails_Activity.class, bundle);
                }
            }
        }
    }

}
